/**
 * Copyright (C) 2009-2010,  Magus Tech. Co., Ltd.
 * \file: nodes.h
 * \author: coanor
 * \date: 2010/4/15
 * \brief: node definition and function declarations
 ******************************************************************************/
#ifndef NODES_H
#define NODES_H

#include "c.h"

typedef enum node_tag_t {
    T_invalid_t = 0,

    /*---------------------
     * TAGS FOR PLAN NODES (plannodes.h)
     *---------------------
     */
    T_plan_t = 10,
    T_existential_t,
    T_result_t,
    T_append_t,
    T_scan_t,
    T_seq_scan_t,
    T_index_scan_t,
    T_ioin_t,
    T_nest_loop_t,
    T_merge_join_t,
    T_hash_join_t,
    T_temp_t,
    T_material_t,
    T_sort_t,
    T_agg_t,
    T_unique_t,
    T_hash_t,
    T_choose_t,
    T_tee_t,
    T_group_t,

    /*---------------------
     * TAGS FOR PRIMITIVE NODES (primnodes.h)
     *---------------------
     */
    T_resdom_t = 100,
    T_fjoin_t,
    T_expr_t,
    T_var_t,
    T_oper_t,
    T_const_t,
    T_param_t,
    T_aggreg_t,
    T_func_t,
    T_array_t,
    T_array_ref_t,

    /*---------------------
     * TAGS FOR INNER PLAN NODES (relation.h)
     *---------------------
     */
    T_rel_t = 200,
    T_path_t,
    T_index_path_t,
    T_JoinPath,
    T_MergePath,
    T_HashPath,
    T_OrderKey,
    T_JoinKey,
    T_MergeOrder,
    T_CInfo,
    T_JoinMethod,
    T_HInfo,
    T_MInfo,
    T_JInfo,
    T_iter_t,
    T_Stream,

    /*---------------------
     * TAGS FOR EXECUTOR NODES (execnodes.h)
     *---------------------
     */
    T_IndexInfo = 300,
    T_RelationInfo,
    T_TupleCount,
    T_TupleTableSlot,
    T_ExprContext,
    T_ProjectionInfo,
    T_JunkFilter,
    T_EState,
    T_BaseNode,
    T_CommonState,
    T_ResultState,
    T_AppendState,
    T_CommonScanState,
    T_ScanState,
    T_IndexScanState,
    T_JoinState,
    T_NestLoopState,
    T_MergeJoinState,
    T_HashJoinState,
    T_MaterialState,
    T_AggState,
    T_GroupState,
    T_SortState,
    T_UniqueState,
    T_HashState,
    T_TeeState,

    /*---------------------
     * TAGS FOR MEMORY NODES (memnodes.h)
     *---------------------
     */
    T_MemoryContext = 400,
    T_GlobalMemory,
    T_PortalMemoryContext,
    T_PortalVariableMemory,
    T_PortalHeapMemory,

    /*---------------------
     * TAGS FOR VALUE NODES (pg_list.h)
     *---------------------
     */
    T_parse_value_t = 500,
    T_list_t,
    T_integer_t,
    T_float_t,
    T_string_t,
    T_Null,

    /*---------------------
     * TAGS FOR PARSE TREE NODES (parsenode.h)
     *---------------------
     */
    T_query_t = 600,
    T_insert_stmt_t,
    T_delete_stmt_t,
    T_update_stmt_t,
    T_CursorStmt,
    T_select_stmt_t,
    T_AddAttrStmt,
    T_AggregateStmt,
    T_ChangeACLStmt,
    T_ClosePortalStmt,
    T_ClusterStmt,
    T_CopyStmt,
    T_CreateStmt,
    T_VersionStmt,
    T_DefineStmt,
    T_DestroyStmt,
    T_ExtendStmt,
    T_FetchStmt,
    T_IndexStmt,
    T_MoveStmt,
    T_ProcedureStmt,
    T_purge_stmt_t,
    T_RecipeStmt,
    T_RemoveFuncStmt,
    T_RemoveOperStmt,
    T_RemoveStmt,
    T_RenameStmt,
    T_rule_stmt_t,
    T_notify_stmt_t,
    T_ListenStmt,
    T_TransactionStmt,
    T_ViewStmt,
    T_LoadStmt,
    T_CreatedbStmt,
    T_DestroydbStmt,
    T_VacuumStmt,
    T_ExplainStmt,

    T_a_expr_t = 700,
    T_attr_t,
    T_a_const_t,
    T_ParamNo,
    T_ident_t,
    T_FuncCall,
    T_a_indices_t,
    T_res_target_t,
    T_ParamString,
    T_time_range_t,
    T_rel_expr_t,
    T_sort_by_t,
    T_range_var_t,
    T_type_name_t,
    T_IndexElem,
    T_ColumnDef,
    T_DefElem,
    T_target_entry_t,
    T_range_tbl_entry_t,
    T_sort_clause_t,
    T_group_clause_t
} node_tag_t;

/**
 * The first field of a node of any type is gauranteed to be the NodeTag.
 * Hence the type of any node can be gotten by casting it to Node. Declaring
 * a variable to be of Node * (instead of void *) can also facilitate
 * debugging.
 */
typedef struct node_t {
    node_tag_t	type;
} node_t;

/**
 * CmdType -
 *    enums for type of operation to aid debugging
 *
 * ??? could have put this in parsenodes.h but many files not in the
 *    optimizer also need this...
 */
typedef enum cmd_type_t {
    CMD_UNKNOWN,
    CMD_SELECT,		/* select stmt (formerly retrieve) */
    CMD_UPDATE,		/* update stmt (formerly replace) */
    CMD_INSERT,		/* insert stmt (formerly append) */
    CMD_DELETE,
    CMD_NOTIFY,
    CMD_UTILITY     /* cmds like create, destroy, copy, vacuum, etc. */
} cmd_type_t;

node_t *new_node(size_t size, node_tag_t tag);

#define node_tag(_node_) ((node_t *)_node_)->type
#define make_node(_node_) (_node_ *)new_node(sizeof(_node_), T_##_node_)
#define node_set_tag(n, t) ((node_t *)n)->type = t
#define is_a(_node_, _tag_) (node_tag(_node_) == T_##_tag_)




#endif // NODES_H
